<?php
/**
 * Description of Controller
 *
 * @author ananda
 */
abstract class Controller extends Kernel
{
    protected $action_col;
    protected $action_cur = 0;

    /**
     * 调用此控制器的应用对象
     * @var App
     */
    public $app;

    /**
     * 父控制器
     * @var Controller
     */
    public $parent;

    /**
     * 子控制器
     * @var Controller
     */
    public $child;

    public $action;
    public $params;

    public $self_name      = 'Controller';
    public $action_list    = array();
    public $action_default = 'Index';

    public function __construct($app, $parent = null) {
        $this->app    = $app;
        $this->parent = $parent;
        $this->init();
    }

    abstract public function init();

    public function setAction($action, $params = array())
    {
        $this->action = $action ? $action : $this->action_default;
        $this->params = $params;
    }

    public function checkAction($action)
    {
        if (!isset($this->action_list[$action])) {
            throw new Exception_Controller("Action [$this->self_name/$action] is not exsits");
        }
    }

    public function callAction($action = null)
    {
        !$action && ($action = $this->action);
        $this->checkAction($action);

        $classname = "Action_{$this->self_name}_$action";
        $this->action_cur++;
        $this->action_col[$this->action_cur] = new $classname();
        $this->action_col[$this->action_cur]->controller = $this;
        $this->action_col[$this->action_cur]->params = $this->params;

        return $this->action_col[$this->action_cur];
    }
}

